package com.eascs.web.p2p.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.p2p.common.entity.LoaneeEntity;
import com.eascs.p2p.common.service.ILoaneeService;
import com.eascs.p2p.common.vo.LoaneeVO;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.tag.MenuTag;
import com.eascs.web.p2p.common.BaseMasterViewController;

@Controller
@RequestMapping("/loaner")
public class LoanerController extends BaseMasterViewController {

	@Reference
	ILoaneeService loaneeService;

	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "ManageLoaner");
	}
	
	@RequestMapping(value = "/index")
	public String index(LoaneeVO vo) {

		vo.setPageSize(10);
		ModelResult<PageResult<Object[]>> model=loaneeService.findPageLoaneeEntity(vo);
		PageResult<Object[]> pageResult=new PageResult<Object[]>();
		if(model.isSuccess()){
			 pageResult = model.getModel();
		} 
		

		String[] titles = { "借款人姓名", "手机号", "身份证","银行卡号" };
		Button[] detailButton = new Button[] { new RedirectButton("修改                   ", "/loaner/updateLoaner?id="),
				new AjaxButton("  查看借款", "/loanerRest/loaner_detail?id=") };
		String findCondition = "";
		String pageUrl = "/loaner/index";

		if (vo != null) {
			String name = vo.getName() == null ? "" : vo.getName();
			String mobile = vo.getName() == null ? "" : vo.getName();

			findCondition = "?name=" + name+"&mobile=" +mobile;
			pageUrl = pageUrl + findCondition;
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl + "&showPage=");
		paginationInfo.setBuildPagingNavigation(true);
		// paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		ModelResult<LoaneeVO> voModel=loaneeService.getLoaneeNum(vo);
		LoaneeVO loaneeVO  =null;
		if(model.isSuccess()){
			loaneeVO = voModel.getModel();
		}
		
		ViewContext.put("loaneeVO", loaneeVO);
		
		ModelResult<Integer> totalAmountModel=loaneeService.getTotalAmount(vo);
		Integer totalAmount=0;
		if(totalAmountModel.isSuccess()){
			totalAmount = totalAmountModel.getModel();
		
		}
		ViewContext.put("totalAmount", totalAmount==null?0:totalAmount);
		BaseRestResult.refresh();
		return "/loaner/index";
	}

	@RequestMapping(value = "/add_loaner")
	public String addLoaner() {
		ViewContext.put("title", "新增");
		return "/loaner/loaner_edit";
	}
	
	@RequestMapping(value = "/updateLoaner")
	public String addLoaner(String id) {
		ModelResult<LoaneeEntity> model = loaneeService.findLoaneeEntityDetail(id);
		LoaneeEntity vo=null;
		if(model.isSuccess()){
			vo = model.getModel();
			ViewContext.put("vo", vo);
		}
		
		ViewContext.put("title", "修改");
		return "/loaner/loaner_edit";
	}
	
	

}
